package com.example.service.impl;

import com.example.pojo.Discuss;
import com.example.pojo.Posts;
import com.example.mapper.PostsMapper;
import com.example.service.IPostsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2024-06-12
 */
@Service
public class PostsServiceImpl extends ServiceImpl<PostsMapper, Posts> implements IPostsService {

    @Resource
    private PostsMapper postsMapper;
    @Resource
    private RedisTemplate<String, List<Posts>> redisTemplate;
    @Override
    public List<Posts> getAll() {
        // 首先尝试从 Redis 缓存中获取数据
        String cacheKey = "allPosts";
        List<Posts> cachedPosts = redisTemplate.opsForValue().get(cacheKey);

        if (cachedPosts == null || cachedPosts != postsMapper.getAll()) {
            // 如果缓存中没有数据，则从数据库或其他持久化存储中获取数据
            List<Posts> posts = postsMapper.getAll(); // 这是从数据库中获取的方法

            // 对获取的数据进行排序，按照 score 字段降序排序
            posts.sort(Comparator.comparingInt(Posts::getScore).reversed());

            // 将排序后的数据存入 Redis 缓存
            redisTemplate.opsForValue().set(cacheKey, posts, Duration.ofMinutes(30)); // 设置缓存时间为2000分钟

            return posts;
        } else {
            // 如果缓存中有数据，直接返回缓存数据
            return cachedPosts;
        }
    }

    @Override
    public void addPost(Integer uid, Integer bid, String title, String content, String images) {
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        //格式转化
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String tdate = now.format(formatter);
        postsMapper.addPost(uid,bid,title,content,images,tdate);
    }

    @Override
    public List<Discuss> getDiscuss(Integer pid) {
        return postsMapper.getDiscuss(pid);
    }

    @Override
    public void addDiscuss(Integer uid, String text, Integer pid) {
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        //格式转化
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String cdate = now.format(formatter);
        postsMapper.addDiscuss(uid,text,cdate,pid);
    }

    @Override
    public List<Posts> getDiscuss_Bar(Integer bid,Integer uid) {
        return postsMapper.getDiscuss_Bar(bid,uid);
    }

    @Override
    public List<Posts> getDiscuss_User(Integer uid) {
        return postsMapper.getDiscuss_User(uid);
    }

    @Override
    public Posts getDiscuss_Pid(Integer pid) {
        return postsMapper.getDiscuss_Pid(pid);
    }
}
